package com.itany.service.impl;

import com.itany.entity.Notice;
import com.itany.entity.NoticeExample;
import com.itany.entity.ServerCompany;
import com.itany.mapper.NoticeMapper;
import com.itany.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper mapper;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Map<String, Object>> queryAll() {
        List<Notice> list = mapper.queryAll();
        List<Map<String, Object>> mapList = new ArrayList<>();
        if(!list.isEmpty()){
            for (Notice notice : list) {
                Map<String, Object> u = new HashMap<>();
                u.put("id",notice.getId());
                u.put("title",notice.getTitle());
                u.put("info",notice.getInfo());
                u.put("managerusername",notice.getManagerusername());
                u.put("username",notice.getUsername());
                u.put("flag",notice.getFlag());
                u.put("createdate",notice.getCreatedate());
                mapList.add(u);
            }
            return mapList;
        }else{
            return null;
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void deleteById(Integer id) {
        Notice notice = new Notice();
        notice.setId(id);
        notice.setFlag(0);
        mapper.updateByPrimaryKeySelective(notice);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void insert(String title, String info, Integer manageruserid) {
        Notice notice = new Notice();
        notice.setTitle(title);
        notice.setInfo(info);
        notice.setManageruserid(manageruserid);
        notice.setFlag(1);
        notice.setCreatedate(new Date());
        mapper.insertSelective(notice);
    }
}
